<?php

namespace Divine\Http\Message;

abstract class AbstractMessage
{
    /**
     * The HTTP version of the message.
     *
     * @var string
     */
    protected $_httpVersion;
    /**
     * An array of HTTP headers where the index is a valid HTTP header field name.
     *
     * @param array
     */
    protected $_headers = array();
    /**
     * A message body.
     *
     * @param mixed
     */
    protected $_body;

    /**
     * HTTP protocol version 1.0
     *
     * @var string
     */
    const PROTOCOL_HTTP_1_0 = 'HTTP/1.0';
    /**
     * HTTP protocol version 1.1
     *
     * @var string
     */
    const PROTOCOL_HTTP_1_1 = 'HTTP/1.1';

    /**
     * Sets the HTTP version of the message.
     *
     * @param string $httpVersion The HTTP version of the message such as "HTTP/1.1".
     * @throws \UnexpectedValueException Thrown when an unknown value is passed for the HTTP version.
     */
    protected function _setHttpVersion($httpVersion)
    {
        if (!static::_isValidHttpVersion($httpVersion))
        {
            throw new \UnexpectedValueException('Unknown HTTP version: ' . $httpVersion);
        }

        $this->_httpVersion = $httpVersion;
    }

    /**
     * Gets the HTTP version of the message.
     *
     * @return string The HTTP version of the message.
     */
    public function getHttpVersion()
    {
        return $this->_httpVersion;
    }

    /**
     * Set the body.
     *
     * @param mixed $body
     */
    protected function _setBody($body)
    {
        $this->_body = $body;
    }

    /**
     * Gets the body.
     *
     * @return mixed
     */
    public function getBody()
    {
        return $this->_body;
    }

    /**
     * Bulk set multiple headers
     *
     * @param array $headers An array of HTTP headers indexed by field name.
     */
    protected function _setHeaders(array $headers)
    {
        $this->_headers = $headers;
    }

    /**
     * Retrieve all headers.
     *
     * @return array
     */
    public function getHeaders()
    {
        return $this->_headers;
    }

    /**
     * Checks for a valid numeric HTTP version.
     *
     * @param string $httpversion The HTTP version of a message such as "HTTP/1.1".
     * @return boolean
     */
    protected static function _isValidHttpVersion($httpVersion)
    {
        return ($httpVersion === self::PROTOCOL_HTTP_1_0 || $httpVersion === self::PROTOCOL_HTTP_1_1);
    }
}